<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class FMIS extends FMIS_Controller
{
	public function __construct() {
		parent::__construct();
        $this->load->model('faculty_user_model');
        $this->load->model('faculty_usergroup_model');
        $this->load->model('faculty_user_model');
	}

	public function index()
	{
        $username = $this->session->userdata('username');
        if($username)
            $this->profile();
        else
		    $this->login();
	}

	public function login()
    {
        if($this->input->server('REQUEST_METHOD') === 'GET')
        {
            //$data = array(
            //   'page_title'  => 'Log-in'
            //);
            $this->load->library('viewmodel/fmis/login_viewmodel');
            $data = $this->login_viewmodel->get();
            $this->setup_view('fmis/login', $data);
            return;
        }
        else
        $username = $this->input->post('username');//$_POST['username'];
        $password = $this->input->post('password');//$_POST['password'];
        $message = $this->input->post('message');//$_POST['password'];

        $retVal = $this->faculty_user_model->get_userPass($username, $password);
        //if($username == 'richard' && $password != 'domingo')
        if($retVal['numRows'] == 0)
        {
            $data = array(
                'page_title' => 'Log in',
                'username' => $username,
                'message' => 'Account not recognized'
            );
            $this->setup_view('fmis/login', $data);
            return;
        }

        /*if($username != 'richard')
        {
            //redirect('home');//$this->set_login_page();
            $data = array(
            'page_title' => 'Log in',
            'username' => '',
            'message' => 'Unknown username'
            );
                $this->setup_view('fmis/login', $data);
            return;
        }*/


        //TODO from database, give necessary values to session
        //but noot all though, ung iba hidden field sa view and then pass by POST
        $newdata = array(
           'username'     => $retVal['data']->FirstName." ".$retVal['data']->LastName,
           'user_id'     => $retVal['data']->MemberID,
           'logged_in' => true,
           'user_type' => $retVal['data']->Name,
           'email' => 'hoh@yeah'
        );

        $this->session->set_userdata($newdata);

        redirect('profile');
    }

    public function profile()
    {
        $data = array(
           'page_title'  => 'Profile'
        );

        //$this->fmis_viewhandler->add_session_data(array('page_title' => 'Profile'));
        if($this->session->userdata('user_type') == 'Regular Members')
        {
            redirect('faculty/viewprofile/'.$this->session->userdata('user_id'));
            //$this->setup_view('faculty/profile', $data);
        }
        else if($this->session->userdata('user_type') == 'Area Head')
        {
            redirect('admin/profile');
            //$this->setup_view('admin/profile', $data);
        //$this->load->view('login', $this->session->all_userdata());
        }
        else
            //session_destroy();
            //echo $this->session->userdata('user_type');
        redirect('home');
    }

    public function logout()
    {
    	session_destroy();
    	redirect('home');
    }

}

/* End of file fmis.php */
/* Location: ./application/controllers/fmis.php */